﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Web.SessionState;
using FJASPX.Framwork.Helper;
using FJASPX.Framwork.Constant;

namespace FJASPX.Framwork.UI
{
    public abstract class BaseHandler : IHttpHandler, IRequiresSessionState 
    {
        protected HttpContext _context;

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            this._context = context;
            this.CallRead();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        protected virtual void CallRead() 
        {

        }

        /// <summary>
        /// 检测管理员登陆
        /// </summary>
        /// <returns>是否已经登陆</returns>
        protected virtual bool IsMLogin()
        {
            return true;
        }

        /// <summary>
        /// 取随机数作为验证码
        /// </summary>
        /// <returns></returns>
        protected string GenerateCheckCode()
        {
            string verCode = StringHelper.RandCN(4);
            StringHelper.SetSession(Strings.VAL_CODE, verCode);
            return verCode;
        }

        /// <summary>
        /// 画出验证码
        /// </summary>
        /// <param name="checkCode">验证码字符串</param>
        protected void CreateCheckCodeImage(string checkCode, double width, double height)
        {
            if (checkCode.IsNull() || checkCode.IsEmpty())
            {
                return;
            }
            this.SetSession(Strings.VAL_CODE, checkCode);
            Bitmap image = new Bitmap((checkCode.Length * width).ToInt(), height.ToInt());
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成随机生成器
                Random random = new Random();
                //清空图片背景色
                g.Clear(Color.White);
                //画图片的背景噪音线
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font(Strings.FONT_ARIAL, 12, (FontStyle.Bold | FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(checkCode, font, brush, 2, 2);
                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                MemoryStream ms = new MemoryStream();
                image.Save(ms, ImageFormat.Gif);
                _context.Response.ClearContent();
                _context.Response.ContentType = Strings.IMAGE_GIF;
                _context.Response.BinaryWrite(ms.ToArray());
            }
            catch (Exception ex)
            {
                Log.WriteLog(Message.ERR, ex);
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

        /// <summary>
        /// 读取session值
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns>值</returns>
        protected object GetSession(string name)
        {
            object Str_Value = null;
            try
            {
                Str_Value = _context.Session[name];
            }
            catch (Exception ex)
            {
                Str_Value = null;
                Log.WriteLog(Message.ERR, ex);
            }
            return Str_Value;
        }

        /// <summary>
        /// 读取session值
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns>值</returns>
        protected string GetStrSession(string name)
        {
            string Str_Value = null;
            try
            {
                Str_Value = _context.Session[name].ToString();
            }
            catch
            {
                Str_Value = String.Empty;
            }
            return Str_Value;
        }

        /// <summary>
        /// 设置session值
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="pObj_Value">值</param>
        protected void SetSession(string name, object value)
        {
            try
            {
                _context.Session[name] = value;
            }
            catch (Exception ex)
            {
                Log.WriteLog(Message.ERR, ex);
            }
        }
    }
}
